/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.resources.joram.wrapper;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;

import org.objectweb.fractal.api.control.IllegalLifeCycleException;
import org.objectweb.fractal.api.control.LifeCycleController;
import org.objectweb.fractal.util.Fractal;

import fr.dyade.aaa.agent.AgentServer;
import fr.jade.base.JadeBaseComponent;
import fr.jade.fraclite.api.control.NoSuchAttributeException;
import fr.jade.resources.joram.api.JndiInterface;

public class JndiWrapper extends JadeBaseComponent implements LifeCycleController, JndiInterface {

	private String listAtt[] = {"jndiPort"};
	
	private boolean started;
	private String hostName;
	private String jndiPort;
	private String a3File;
	
	private void generateA3ServerXML(){
		try {
			OutputStream fout = new FileOutputStream("a3servers.xml");
			OutputStream bout = new BufferedOutputStream(fout);
			OutputStreamWriter configWriter = new OutputStreamWriter(bout);
			a3File = "<?xml version=\"1.0\"?>" + "\n";
			a3File = a3File + "<config>" + "\n";
			a3File = a3File + "<property name=\"Transaction\" value=\"fr.dyade.aaa.util.NullTransaction\"/>\r\n";
			a3File = a3File + "<server id=\"100\" name=\"S100\" hostname=\"" + hostName + "\">" + "\n";
			a3File = a3File + "<service class=\"fr.dyade.aaa.jndi2.server.JndiServer\" args=\"" + jndiPort + "\"/>" + "\n";
			a3File = a3File + "</server>" + "\n";
			a3File = a3File + "</config>" + "\n";
			configWriter.write(a3File);
			configWriter.flush();
			configWriter.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String getFcState() {
		return started ? LifeCycleController.STARTED : LifeCycleController.STOPPED;
	}

	public void startFc() throws IllegalLifeCycleException {
	  if(!started){
  		started = true;
  		try{
  			// Change our internal name
  			Fractal.getNameController(self).setFcName("JndiRegistry");
  			
  			hostName = InetAddress.getLocalHost().getCanonicalHostName();
  			generateA3ServerXML();
  			System.out.println("JNDI server on " + hostName + ":" + jndiPort + " starting...");
  			AgentServer.init(new String[] {"100", "S100"});
  			AgentServer.start();
  			//JndiServer.init(jndiPort, true);
  			System.out.println("JNDI server [started]");
  		} catch (Exception e) {
  			e.printStackTrace();
  		}
	  }
	}

	public void stopFc() throws IllegalLifeCycleException {
	  if(started){
  		started = false;
  		AgentServer.stop();
	  }
	}

	public String getAttribute(String name) throws NoSuchAttributeException {
		if (name.equals("jndiPort"))
			return this.jndiPort;
		else
			return super.getAttribute(name);
	}

	public String[] listFcAtt() {
		return super.concat(listAtt, super.listFcAtt());
	}

	public void setAttribute(String name, String value) throws NoSuchAttributeException {
		if (name.equals("jndiPort"))
			this.jndiPort = value;
		else
			super.setAttribute(name, value);
	}

	public String getHost() {
		return hostName;
	}

	public String getPort() {
		return jndiPort;
	}
}
